package com.neusoft.a132wxyorderingdish.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.activity.FoodInfoActivity;
import com.neusoft.a132wxyorderingdish.activity.FoodListActivity;
import com.neusoft.a132wxyorderingdish.beans.CollectBean;
import com.neusoft.a132wxyorderingdish.beans.ResultBean;
import com.neusoft.a132wxyorderingdish.common.Constants;
import com.neusoft.a132wxyorderingdish.listener.RetrofitListener;
import com.neusoft.a132wxyorderingdish.model.ShopModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements RetrofitListener<ResultBean> {
    private Context context;
    private List<CollectBean> collectBeans;
    private LayoutInflater layoutInflater;
    private int shop_id,food_id;

    public CollectAdapter(Context context,List<CollectBean> collectBeans){
        this.collectBeans=collectBeans;
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_collect,viewGroup,false);
        CollectViewHolder holder=new CollectViewHolder(v);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        final CollectBean collectBean=collectBeans.get(i);
        CollectViewHolder collectViewHolder=(CollectViewHolder)viewHolder;
        if(collectBean.getFlag().equals("0")){
            collectViewHolder.collect_name.setText(collectBean.getShopname());
            collectViewHolder.collect_address.setText(collectBean.getAddress());
            if(!collectBean.getPic().equals("")){
                Picasso.get().load(Constants.BASE_URL+collectBean.getPic()).into(collectViewHolder.collect_img);
            }

            collectViewHolder.collect_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context,FoodListActivity.class);
                    intent.putExtra("shop_id",collectBean.getShop_id());
                    context.startActivity(intent);
                }
            });

            collectViewHolder.collect_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    uncollectShop(String.valueOf(collectBean.getShop_id()),String.valueOf(collectBean.getUser_id()));
                }
            });
        }

        else{
            collectViewHolder.collect_name.setText(collectBean.getFoodname());
            collectViewHolder.collect_address.setText(collectBean.getPrice()+"元");
            if(!collectBean.getPic().equals("")){
                Picasso.get().load(Constants.BASE_URL+collectBean.getPic()).into(collectViewHolder.collect_img);
            }

            collectViewHolder.collect_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context, FoodInfoActivity.class);
                    intent.putExtra("food_id",collectBean.getFood_id());
                    context.startActivity(intent);
                }
            });

            collectViewHolder.collect_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    uncollectFood(String.valueOf(collectBean.getFood_id()),String.valueOf(collectBean.getUser_id()));
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (collectBeans == null){

            return 0;
        }
        return collectBeans.size();

    }

    @Override
    public void onSuccess(ResultBean resultBean, int flag) {
        if(flag== Constants.USERCOLLECTFOOD) {
            if (resultBean.getSuccess().equals("1")) {
                Toast.makeText(context, "取消收藏菜单成功", Toast.LENGTH_SHORT).show();
            }
            for (int i = 0; i < collectBeans.size(); i++) {
                CollectBean object = collectBeans.get(i);
                if (object.getFood_id()==food_id) {
                    collectBeans.remove(object);
                    notifyDataSetChanged();
                    break;
                }
            }
        }

        else{
            if(resultBean.getSuccess().equals("1")) {
                Toast.makeText(context, "取消收藏店铺成功", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < collectBeans.size(); i++) {
                    CollectBean object = (CollectBean) collectBeans.get(i);
                    if (object.getShop_id()==shop_id) {
                        collectBeans.remove(object);
                        notifyDataSetChanged();
                        break;
                    }
                }
            }
            else{
                Toast.makeText(context, "取消收藏店铺失败", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void onFail() {

    }

    public class CollectViewHolder extends RecyclerView.ViewHolder{
        ImageButton collect_img;
        TextView collect_back;
        TextView collect_name,collect_address;
        public CollectViewHolder(View itemView) {
            super(itemView);
            collect_img=itemView.findViewById(R.id.collect_image);
            collect_back=itemView.findViewById(R.id.collect_back);
            collect_address=itemView.findViewById(R.id.collect_address);
            collect_name=itemView.findViewById(R.id.collect_name);
        }
    }

    //取消收藏店铺
    private void uncollectShop(String shop_id,String user_id){

        ShopModel shopModel=new ShopModel();
        shopModel.userCollectShop(user_id,shop_id,this);
    }


    //取消收藏菜单
    private void uncollectFood(String food_id,String user_id){

        ShopModel shopModel=new ShopModel();
        shopModel.userCollectFood(user_id,food_id,this);
    }

}

